#pragma once
#include <vector>
#include <torch/torch.h>
#include "VTECConverter.h"

class SphericalHarmonic {
public:
    SphericalHarmonic(int maxDegree = 8);
    ~SphericalHarmonic();

    // 设置VTEC观测数据
    void setVTECObservations(const std::vector<VTECObservation>& vtecObs);
    
    // 拟合球谐函数模型
    void fitModel();
    
    // 预测指定位置的VTEC
    double predictVTEC(double latitude, double longitude, double time);
    
    // 获取球谐系数
    torch::Tensor getCoefficients() const;
    
    // 获取球谐系数向量
    std::vector<double> getCoefficientsVector() const;
    
    // 设置球谐系数
    void setCoefficients(const std::vector<double>& coeffs);
    
    // 评估指定位置的VTEC
    double evaluateVTEC(double latitude, double longitude) const;
    
    // 生成全球VTEC网格
    torch::Tensor generateGlobalVTECGrid(int latRes = 2, int lonRes = 2) const;

private:
    std::vector<VTECObservation> vtecObservations_;
    int maxDegree_;
    torch::Tensor coefficients_;  // 球谐系数
    
    // 计算勒让德函数
    torch::Tensor calculateLegendre(const torch::Tensor& x, int n, int m) const;
    
    // 计算勒让德多项式值(标量版本)
    double calculateLegendreValue(int n, int m, double x) const;
    
    // 构建设计矩阵
    torch::Tensor buildDesignMatrix() const;
    
    // 将观测数据转换为torch张量
    std::tuple<torch::Tensor, torch::Tensor> observationsToTensor() const;
};